// pages/company-account/companyAccount.js
import Ajax from '../../common/Ajax.js';
import moment from '../../common/moment';


const sessionId = wx.getStorageSync('sessionId');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        companyAcountList: [],
        value: "",
        pageNo: 1,
        pageSize: 10,
        isBorder: false,
    },

    toEdit: function(e) {
        const { id } = e.currentTarget;
        wx.navigateTo({
            url: `../edit-account/editAccount?id=${id}&sign=edit`,
        })
    },

    toAddCompanyAccount: function() {
        wx.navigateTo({
          url: '../edit-account/editAccount',
        })
    },

    onChange: function(e) {
        this.setData({value: e.detail})
    },

    onClick: function() {
        const { value } = this.data;
        this.getCompanyAccount({pageNo: 1, pageSize: 10, accountName: value, sign: "reset"})
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        this.getCompanyAccount({pageNo: 1, pageSize: 10})
    },

    getCompanyAccount: function({pageNo, pageSize, accountName, sign}) {
        Ajax.post("/companyAccounts/page", {
            pageNo, 
            pageSize, 
            likes: {accountName},
            orders: {createdTime: "desc"},
            deleteFlag: 0,
        }, sessionId)
            .then(res => {
                res.data.data.forEach(item => {
                    item.createdTime = moment(item.createdTime).format("YYYY-MM-DD HH:mm:ss")
                })
                if(res.code === "0") {
                    if(sign) {
                        this.setData({
                            companyAcountList: res.data.data,
                            isBorder: res.data.data.length < 10,
                            pageNo,
                            pageSize,
                        })
                    } else {
                        let { companyAcountList } = this.data;
                        companyAcountList = companyAcountList.concat(res.data.data)
                        this.setData({
                            companyAcountList,
                            isBorder: res.data.data.length < 10,
                            pageNo,
                            pageSize,
                        })
                    }
                }
            })
            .catch(err => {
                throw err;
            })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let {pageNo, pageSize, value: accountName } = this.data;
        pageNo = pageNo + 1;
        this.getCompanyAccount({pageNo, pageSize, accountName})
    },

    toDelete: function(e) {
        const { id } = e.currentTarget;
        const { value } = this.data;
        wx.showLoading({
          title: '删除中....',
        })
        Ajax.get(`/companyAccounts/deleteCompanyAccount?companyAccountId=${id}`, {}, sessionId)
            .then(res => {
                if(res.code === "0") {
                    this.getCompanyAccount({pageNo: 1, pageSize: 10, accountName: value, sign: "reset"})
                }
            })
            .catch(err => {
                throw err;
            })
            .finally(() => {
                wx.hideLoading();
            })
    },
})